package com.simon.annual.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import com.simon.annual.entity.ReportIncomeanalysi;
import com.simon.annual.entity.ReportIncomestructure;
import com.simon.annual.entity.ReportNeeq;
import com.simon.annual.entity.ReportPartner;
import com.simon.annual.repositoy.IReportIncomeanalysiRepositoy;
import com.simon.annual.repositoy.IReportIncomestructureRepositoy;
import com.simon.annual.repositoy.IReportPartnerRepositoy;
import com.simon.annual.service.ICompanyPublicInfo;

/**
 * 第四节:管理层讨论与分析
 * */
@Service
public class AbortManagerImpl{

	@Resource
	private ICompanyPublicInfo companyPublicInfo;
	@Resource
	private IReportPartnerRepositoy reportPartnerRepositoy;
	@Resource
	private IReportIncomeanalysiRepositoy reportIncomeanalysiRepositoy;
	@Resource
	private IReportIncomestructureRepositoy reportIncomestructureRepositoy;
	
	
	public ReportNeeq analysis(Element na, ReportNeeq reportNeeq, int companyPtrId) {
//		Element  na = pElements.get(3);
		//商业模式
		Element busi = companyPublicInfo.findNextTag("table",na);
		reportNeeq.setBusinessModel(busi.text());
		//年度内变化统计
		Element changes = companyPublicInfo.findNextTag("table",busi);
		Elements tds = changes.select("table tr > *");
		reportNeeq.setIndustryChanged(tds.get(3).text());
		reportNeeq.setMainBusinessChanged(tds.get(5).text());
		reportNeeq.setMainProductChanged(tds.get(7).text());
		reportNeeq.setClientTypeChanged(tds.get(9).text());
		reportNeeq.setKeyResourceChanged(tds.get(11).text());
		reportNeeq.setSalesChannelChanged(tds.get(13).text());
		reportNeeq.setIncomeSourceChanged(tds.get(15).text());
		reportNeeq.setBusinessModelChanged(tds.get(17).text());
		//报告期内经营情况回顾
		Element performance = companyPublicInfo.findNextTag("table",changes);
		reportNeeq.setPerformanceReview(performance.text());
		//主营业务分析
		Element mainElement = companyPublicInfo.findNextTag("table",performance);
		//项目重大变动原因
		Element sca = companyPublicInfo.findNextTag("table",mainElement);
		reportNeeq.setPl_SCA(sca.text());
		//收入构成
		Element incomeStructure = companyPublicInfo.findNextTag("table",sca);
		List<ReportIncomestructure> incomestructureList = new ArrayList<ReportIncomestructure>();
		Elements  intrs = incomeStructure.select("table tr");
		for (int i = 0; i < intrs.size()-1; i++) {
			if(i!=0){
				ReportIncomestructure incomestructure = new ReportIncomestructure();
				Elements intds = intrs.get(i).children();
				incomestructure.setName(intds.get(0).text());
				incomestructure.setRevenueThisPeriod(intds.get(1).text());
				incomestructure.setCostThisPeriod(intds.get(2).text());
				incomestructure.setRevenueLastPeriod(intds.get(3).text());
				incomestructure.setCostLastPeriod(intds.get(4).text());
				incomestructure.setProjectScopeId(companyPtrId);
				incomestructureList.add(incomestructure);
			}
		}
		reportIncomestructureRepositoy.save(incomestructureList);
		//按产品或区域分类分析
		Element incomeAnalysis  = companyPublicInfo.findNextTag("table",incomeStructure);
		List<ReportIncomeanalysi> reportIncomeanalysiList = new ArrayList<ReportIncomeanalysi>();
		Elements  inctrs = incomeAnalysis.select("table tr");
		for (int i = 1; i < inctrs.size(); i++) {
			if(inctrs.get(i).children().size()>2){
				ReportIncomeanalysi reportIncomeanalysi =new ReportIncomeanalysi();
				Elements inctds = inctrs.get(i).children();
				reportIncomeanalysi.setName(inctds.get(0).text());
				reportIncomeanalysi.setRevenueThisPeriod(inctds.get(1).text());
				reportIncomeanalysi.setPctThisPeriod(inctds.get(2).text());
				reportIncomeanalysi.setRevenueLastPeriod(inctds.get(3).text());
				reportIncomeanalysi.setPctLastPeriod(inctds.get(4).text());
				reportIncomeanalysi.setProjectScopeId(companyPtrId);
				reportIncomeanalysiList.add(reportIncomeanalysi);
			}
		}
		reportIncomeanalysiRepositoy.save(reportIncomeanalysiList);
		//收入构成变动的原因
		Element income_SCA = companyPublicInfo.findNextTag("table",incomeAnalysis);
		reportNeeq.setIncome_SCA(income_SCA.text());
		//现金流量状况
		Element crash = companyPublicInfo.findNextTag("table",income_SCA);
		//现金流量分析
		Element cf_SCA = companyPublicInfo.findNextTag("table",crash);
		reportNeeq.setCf_SCA(cf_SCA.text());
		//主要客户情况 
		Element clients = companyPublicInfo.findNextTag("table",cf_SCA);
		clients.select("table tr").last().remove();
		//如存在关联关系，则必须披露客户的具体名称
		Element main = companyPublicInfo.findNextTag("table",clients);
		if(main.select("table tr").last().children().first().text().indexOf("\u5408\u8BA1")==0
				||main.select("table tr").last().children().size()==1){
			main =clients;
		}
		//主要供应商情况 
		main = companyPublicInfo.findNextTag("table",main);
		main.select("table tr").first().remove();
		clients.append(main.html());
		List<ReportPartner> reportPartnerList = new ArrayList<ReportPartner>();
		Elements cliente =  clients.select("table tr");
		for (int i = 1; i < cliente.size(); i++) {
			ReportPartner reportPartner =new ReportPartner();
			Elements ctds = cliente.get(i).children();
			if(ctds.size()==5){
				reportPartner.setName(ctds.get(1).text());
				reportPartner.setTransactionValue(ctds.get(2).text());
				reportPartner.setTransactionPct(ctds.get(3).text());
				reportPartner.setIsAffiliated(ctds.get(4).text());
				reportPartner.setProjectScopeId(companyPtrId);
				reportPartnerList.add(reportPartner);
			}
		}
		reportPartnerRepositoy.save(reportPartnerList);
		//如存在关联关系，则必须披露供应商的具体名称
		Element rnd =companyPublicInfo.findNextTag("table",main);
		if(rnd.select("table tr").last().children().size()>1){
			rnd = main;
		}
		//研发支出与专利
		rnd = companyPublicInfo.findNextTag("table",rnd);
		Elements rnds = rnd.select("table tr > *");
		reportNeeq.setRndThisPeriod(rnds.get(4).text());
		reportNeeq.setRndLastPeriod(rnds.get(5).text());
		reportNeeq.setRndPctThisPeriod(rnds.get(7).text());
		reportNeeq.setRndPctLastPeriod(rnds.get(8).text());
		//研发支出分析
		Element zh = companyPublicInfo.findNextTag("table",rnd);
		if(zh.select("table tr").last().children().size()>1){
			zh = rnd;
		}
		//资产负债结构分析
		zh = companyPublicInfo.findNextTag("table",zh);
		//资产负债项目重大变动原因
		Element bs_SCA = companyPublicInfo.findNextTag("table", zh);
		reportNeeq.setBs_SCA(bs_SCA.text());
		//主要控股子公司、参股公司情况
		Element subsidiary = companyPublicInfo.findNextTag("table", bs_SCA);
		reportNeeq.setMajorSubsidiaryOrAssociatedReview(subsidiary.html());
		//委托理财及衍生品投资情况
		Element entrusted = companyPublicInfo.findNextTag("table", subsidiary);
		reportNeeq.setEntrustedOrDerivativesReview(entrusted.html());
		//外部环境的分析
		Element exterior = companyPublicInfo.findNextTag("table", entrusted);
		reportNeeq.setExteriorEnvAnalysis(exterior.text());
		//竞争优势分析
		Element competition = companyPublicInfo.findNextTag("table", exterior);
		reportNeeq.setCompetitionAdvantagesAnalysis(competition.text());
		//持续经营评价
		Element going = companyPublicInfo.findNextTag("table", competition);
		reportNeeq.setGoingConcernAnalysis(going.text());
		//扶贫与社会责任
//		Element social = xml.findNextTag("table", going);
//		social = xml.getNextTag(social,social);
//		reportNeeq.setSocialResponsibilityReview(social.text());
		Element voluntary = going.nextElementSibling();
		if(voluntary.text().indexOf("\u516D")>-1){
			//自愿披露
			voluntary = companyPublicInfo.findNextTag("table", going);
			reportNeeq.setVoluntaryDisclosure(voluntary.text());
			voluntary = voluntary.nextElementSibling();
		}
		Element uncertainty = voluntary;
		if(uncertainty.text().indexOf("\u4E8C")>-1){
			//未来展望(自愿披露)
			//行业发展趋势
			if(uncertainty.nextElementSibling().text().indexOf("\u884C\u4E1A\u53D1\u5C55")>-1){//行业发展
				uncertainty = companyPublicInfo.findNextTag("table", voluntary);
				reportNeeq.setIndustryTrends(uncertainty.text());
			}if(uncertainty.nextElementSibling().text().indexOf("\u516C\u53F8\u53D1\u5C55")>-1){//公司发展
				//公司发展战略
				uncertainty = companyPublicInfo.findNextTag("table", uncertainty);
				reportNeeq.setDevelopmentStrategy(uncertainty.text());
			}if(uncertainty.nextElementSibling().text().indexOf("\u98CE\u9669\u56E0\u7D20")<0){//风险因素
				//经营计划或目标
				uncertainty = companyPublicInfo.findNextTag("table", uncertainty);
				reportNeeq.setBusinessPlan(uncertainty.text());
			}if(uncertainty.nextElementSibling().text().indexOf("\u98CE\u9669\u56E0\u7D20")<0){//风险因素
				//不确定性因素
				uncertainty = companyPublicInfo.findNextTag("table", uncertainty);
				reportNeeq.setBusinessPlan(uncertainty.text());
			}
			
		}
		//三、风险因素
		//持续到本年度的风险因素
		Element risk = companyPublicInfo.findNextTag("table", uncertainty);
		reportNeeq.setRiskLasted(risk.text());
		//报告期内新增的风险因素
		Element riskNew = companyPublicInfo.findNextTag("table", risk);
		reportNeeq.setRiskNew(riskNew.text());
		//非标准审计意见说明
		Element mao = companyPublicInfo.findNextTag("table", riskNew);
		Elements maotr = mao.select("table tr");
		reportNeeq.setIs_MAO(maotr.get(0).child(1).text());
		reportNeeq.setAuditOpinion(maotr.get(1).child(1).text());
		reportNeeq.setBoard_statement_on_MAO(maotr.last().text());
		//关键事项审计说明
//		Element statement = xml.findNextTag("table", mao);
//		statement = xml.getNextTag(statement,statement);
//		reportNeeq.setStatement_on_KIAO(statement.text());
		return reportNeeq;
	}

}
